package com.sara.web;

import com.google.gson.Gson;
import com.sara.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class BaseServlet extends HttpServlet {

    protected void forword(String path, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/views/" + path + ".jsp").forward(req,resp);
    }

    protected void sendJson(Object obj, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(new Gson().toJson(obj));

        writer.flush();
        writer.close();

    }

    protected Admin getAdmin(HttpServletRequest req){
        return (Admin) req.getSession().getAttribute("admin");
    }

}
